/*
 * ------------------------------------------------------------------------
 *  FECHA      HORA     NOMBRE     COMENTARIO     			     
 * ------------------------------------------------------------------------
 *  21/05/08   16:58    ALEX       Creación e implementación de la clase.              
 *  30/05/08   17:24    ALEX       Afegida la documentació pendent.
 * 
 */

package net.ieslaferreria.infoCiutat;

/**
 *
 * @author Alex, Javi, David y Sergio
 */
public class Math {
    
    /**
     * Square root implemented with integer math, ie the argument and the
     * result are both integers.
     * The result of SQRT(x), where x is an integer >= 0, is identical
     * to the result of the expression (int)java.lang.Math.sqrt(x)for J2SE
     * Note:  for x<0 sqrt(x) returns 0!
     * 
     * @param	integer x	
     */
    public static int sqrt(int x){
        // The method DebugME can not be used in a static method and so we
        // have retained some trace System.out.println for debugging
        /*
         * First, find the power of 100, nd, such that x/nd yields the one,
         * if x has odd number of digits, or two most significant digits of x.
         */
        int nd = 0;
        int n = x;
        int t = 1;
        while (n > 0){
            nd =t;
            t *= 100;
            n =x / t;
            //System.out.println("SQRT n, t "+n+", "+t);
        }
        //System.out.println("SQRT x, nd "+x+", "+nd);
        /*
         * Second, consider the most significant digits of x starting with
         * x/nd and stepping nd, dividing by 100, then adding pairs of
         * digits until the least significant digits are done.
         */
        int d ; // will contain the most significant digits of the answer
        t = 0;
        int iter = 0;		// just to keep track of innermost loop
        while (nd > 0){
            n = x / nd;
            d = 10 * t;
            //System.out.println("SQRT, in begin while n, d "+n+", "+d);
            /* In the for loop we step d until its square is larger than
             * x. We save the prevous value in t, which thus is our answer:
             * the largest number whose square is less than x.
             */
            for (int i = d; i < d+10; i++){
                iter++;
                if (square(i) > n) break;
                t = i;
            }	// end for
            nd /= 100;
        }
        return t;
    }	// end SQRT
    
    /**
     * Retorna el valor absolut del paramtre.
     * 
     * @param x
     * @return int
     */
    public static int abs(int x){
        return ((x < 0)? -x: x);
    }
    
    /**
     * Retorna el valor del parametre elevat al quadrat.
     * 
     * @param x
     * @return int
     */
    public static int square(int x){
        return x*x;
    }
        
   /**
    * Calcula la distancia entre les coordenades passades per paramtre.
    * 
    * @return int
    */
    /*
    * Agafa les coordenades dels parametres i calcula la distancia en
    * linia recta que hi ha entre les dues coordenades.
    *
    * Per calcular la distancia entre dos punts restem les seves
    * coordenades (x2,y2) - (x1,y1) = (x,y) i apliquem la següent
    * formula:
    * d = sqrt(x^2 + y^2)
    */
    public static int calculaDistancia(int x1, int x2, int y1, int y2) {
        int d1 = Math.abs(Math.square(x2-x1));
        int d2 = Math.abs(Math.square(y2-y1));
        return Math.sqrt(d1 - d2);
    }

}
